//
//  SKUDefultQuestionNewTB.m
//  ZITHER
//
//  Created by weixin on 2018/9/2.
//  Copyright © 2018年 weixn. All rights reserved.
//

#import "SKUDefultQuestionNewTB.h"
#import "SKUDefultQuestionTBCell.h"
#import "SKUDetailTakeinVC.h"
#import "SKUDetailReceiveVC.h"
#import "MainRefreshBezierPathV.h"//刷新询价 扇形图
#import "SKUDefultQuestionTBNewCellText.h"
#import "SKUDefultQuestionUserimageNewTBCell.h"
#import "SKUDefultQuestionHeadCell.h"
#import "SKUDefultQuestionNoMoreQuoteCell.h"
#import "AcceptPriceListModel.h"
#import "SKU_DetailTakeinDefault_Cell_WX.h"
#import "SKU_defultQuestionTitle_Cell_WX.h"

/** 第三方库新增 */
#import "STRefreshHeader.h"
#import "UIView+STFrame.h"
#import "SwipeTableView.h"



@interface SKUDefultQuestionNewTB ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,assign)NSInteger itemIndex;
@property (nonatomic,assign)NSInteger numberOfRows;
@property (nonatomic,assign)NSInteger num;//数组总数
@property (nonatomic,assign)bool select;//

@end




@implementation SKUDefultQuestionNewTB



- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self defaultConfiguration] ;
        
    }
    return self;
}

-(void)defaultConfiguration{
    self.delegate = self;
    self.dataSource = self;
    self.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.rowHeight = UITableViewAutomaticDimension;
    self.estimatedRowHeight = 1000;
    
    [self registerNib:[UINib nibWithNibName:@"SKUDefultQuestionUserimageNewTBCell" bundle:nil] forCellReuseIdentifier:@"SKUDefultQuestionUserimageNewTBCell"];
    [self registerNib:[UINib nibWithNibName:@"SKUDefultQuestionTBNewCellText" bundle:nil] forCellReuseIdentifier:@"SKUDefultQuestionTBNewCellText"];
    [self registerNib:[UINib nibWithNibName:@"SKUDefultQuestionNoMoreQuoteCell" bundle:nil] forCellReuseIdentifier:@"SKUDefultQuestionNoMoreQuoteCell"];
    [self registerNib:[UINib nibWithNibName:@"SKUDefultQuestionHeadCell" bundle:nil] forCellReuseIdentifier:@"SKUDefultQuestionHeadCell"];
    [self registerNib:[UINib nibWithNibName:@"SKU_DetailTakeinDefault_Cell_WX" bundle:nil] forCellReuseIdentifier:@"SKU_DetailTakeinDefault_Cell_WX"];
    [self registerNib:[UINib nibWithNibName:@"SKU_defultQuestionTitle_Cell_WX" bundle:nil] forCellReuseIdentifier:@"SKU_defultQuestionTitle_Cell_WX"];


    
//    [self loadList];
   
    /** 第三方库新增 */
//    self.header = [STRefreshHeader headerWithRefreshingBlock:^(STRefreshHeader *header) {
//        [self refreshData];
//
//    }];
}

-(void)refreshData{
    [[serviceDefualt loadskuDetail:self.goodId reponseObjctClass:[skuDetailModel class]]subscribeNext:^(ResponseData* x) {
        if (x.success) {
            self.model  = (skuDetailModel*)x.responseData;
//            [self.header endRefreshing];
//            [self loadList];
        }else{
             [Tools showTips:x.errorInfo];
//            [self.header endRefreshing];
        }
    }];
}



-(void)setGoodId:(NSString *)goodId{
    _goodId = goodId;
    [self refreshData];
}

-(void)setModel:(skuDetailModel *)model{
    _model = model;
    if ([model.myEnquiryFlag isEqualToString:@"1"]) {
        self.mj_header.ignoredScrollViewContentInsetTop = kScreenW/350.0*300+40;
    }else{
        self.mj_header.ignoredScrollViewContentInsetTop = kScreenW/350.0*300;
    }
    [self reloadData];
}

- (void)refreshWithData:(id)numberOfRows atIndex:(NSInteger)index {
    _numberOfRows = [numberOfRows integerValue];
    _itemIndex = index;
//    [Tools showTips:@"走了 refreshWithData:(id)numberOfRows 这方法"];
    [self reloadData];
}

#pragma MARK - 数据源
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

    NSInteger index = 1;
    
    int conut =  [self.model.acceptPriceCount intValue];
    
    if (conut>0) {
        index++;
    }
    
    conut =  [self.model.openSourceCount intValue];
    if (conut>0) {
        //展示提示更多商家向我报价
        index++;
    }

    conut =  [self.model.receivePriceCount intValue];
    index++;//没有就展示未收到 有就展示收到报价整个列表 以及标题列表

    if (conut>0) {
        index += self.GetBizPriceInfoArrM.count;
    }
    self.num = index;
    
    return index;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    /* TODO: ================= 判断 cell 样式 =================*/
    bool acceptPrice =  [self.model.acceptPriceCount intValue]>0;
    bool openSource =  [self.model.openSourceCount intValue]>0;

    if (indexPath.row == 0) {
        return [self getSKUDefultQuestionHeadCell:indexPath.row];
    }else if (indexPath.row == 1){
        if (acceptPrice) {
            return [self getUITableViewCell:0];
        }else{
            if (openSource) {
                //0 已接受报价 1 收到 2提醒
                return  [self getUITableViewCell:2];
            }else{
                return  [self getSKUDefultQuestionNoMoreQuoteCell:0];
            }
        }
    }else if (indexPath.row == 2){
        if (acceptPrice) {
            if (openSource) {
                return  [self getUITableViewCell:2];
            }else{
                return  [self getSKUDefultQuestionNoMoreQuoteCell:0];
            }
        }else{
            return  [self getSKUDefultQuestionNoMoreQuoteCell:0];

//            return [self getSKU_defultQuestionTitle_Cell_WX:indexPath];
        }
    }else{
        if (acceptPrice && indexPath.row ==3) {
            return [self getSKU_defultQuestionTitle_Cell_WX:indexPath];
        }else{
            return [self getSKU_DetailTakeinDefault_Cell_WX:indexPath];
        }
    }
}


-(UITableViewCell*)getSKUDefultQuestionNoMoreQuoteCell:(NSInteger)index{
    static NSString *sectionCell = @"SKUDefultQuestionNoMoreQuoteCell";
    SKUDefultQuestionNoMoreQuoteCell *cell = [self dequeueReusableCellWithIdentifier:sectionCell];
    if (cell == nil) {
        cell = [[SKUDefultQuestionNoMoreQuoteCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:sectionCell];
    }
    cell.selectionStyle = UITableViewCellSeparatorStyleNone;
//    cell.model = self.model;
    //        [cell setIndexCell:indexPath.row];
    
    return cell;
}
-(SKU_DetailTakeinDefault_Cell_WX*)getSKU_DetailTakeinDefault_Cell_WX:(NSIndexPath *)indexPath{
    static NSString *sectionCell = @"SKU_DetailTakeinDefault_Cell_WX";
    SKU_DetailTakeinDefault_Cell_WX *cell = [self dequeueReusableCellWithIdentifier:sectionCell];
    if (cell == nil) {
        cell = [[SKU_DetailTakeinDefault_Cell_WX alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:sectionCell];
    }
    
//    NSLog(@"indexPath.row = %ld",indexPath.row);
//    NSLog(@"index = %ld",indexPath.row +1+self.GetBizPriceInfoArrM.count -self.num);
    
    NSInteger index = indexPath.row +self.GetBizPriceInfoArrM.count -self.num;

    AcceptPriceListModel *model = nil;

    if (self.select) {
        model = self.GetBizPriceInfoSortArrM[index];;
    }else{
        model = self.GetBizPriceInfoArrM[index];;
    }
    
    
    
    

    cell.model = model;
    
    cell.selectionStyle = UITableViewCellSeparatorStyleNone;
    
    return cell;
}

-(SKU_defultQuestionTitle_Cell_WX*)getSKU_defultQuestionTitle_Cell_WX:(NSIndexPath *)indexPath{
    static NSString *sectionCell = @"SKU_defultQuestionTitle_Cell_WX";
    SKU_defultQuestionTitle_Cell_WX *cell = [self dequeueReusableCellWithIdentifier:sectionCell];
    if (cell == nil) {
        cell = [[SKU_defultQuestionTitle_Cell_WX alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:sectionCell];
    }
    
    cell.selectionStyle = UITableViewCellSeparatorStyleNone;
    cell.lab.text = [NSString stringWithFormat:@"%ld个待接受报价",self.GetBizPriceInfoArrM.count];
    
    [[cell.btn addTag]subscribeNext:^(id  _Nullable x) {
        
        self.GetBizPriceInfoSortArrM = [[NSMutableArray alloc]initWithArray:self.GetBizPriceInfoArrM];
        [self.GetBizPriceInfoSortArrM sortUsingComparator:^NSComparisonResult(id  _Nonnull obj1, id  _Nonnull obj2) {
            AcceptPriceListModel *model1 = obj1;
            AcceptPriceListModel *model2 = obj2;
            return [model1.goodsSourcePrice intValue] <= [model2.goodsSourcePrice intValue];
        }];
        
        self.select = !self.select;
        [self reloadData];

        NSLog(@"");
    }];
    return cell;
}


-(UITableViewCell*)getSKUDefultQuestionHeadCell:(NSInteger)index{
    static NSString *sectionCell = @"SKUDefultQuestionHeadCell";
    SKUDefultQuestionHeadCell *cell = [self dequeueReusableCellWithIdentifier:sectionCell];
    if (cell == nil) {
        cell = [[SKUDefultQuestionHeadCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:sectionCell];
    }
    cell.selectionStyle = UITableViewCellSeparatorStyleNone;
    cell.model = self.model;
    //        [cell setIndexCell:indexPath.row];
    [cell setBlock:^{
        if (self.block) {
            self.block();
        }
    }];
    

    return cell;
}

//-(UITableViewCell*)getSKUDefultQuestionHeadCell:(NSInteger)index{
//    static NSString *sectionCell = @"SKUDefultQuestionHeadCell";
//    SKUDefultQuestionHeadCell *cell = [self dequeueReusableCellWithIdentifier:sectionCell];
//    if (cell == nil) {
//        cell = [[SKUDefultQuestionHeadCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:sectionCell];
//    }
//    cell.selectionStyle = UITableViewCellSeparatorStyleNone;
//    cell.model = self.model;
//    //        [cell setIndexCell:indexPath.row];
//    [cell setBlock:^{
//        if (self.block) {
//            self.block();
//        }
//    }];
//
//
//    return cell;
//}

-(UITableViewCell*)getUITableViewCell:(NSInteger)index{
    //0 已接受报价 1 收到 2提醒

    if (index == 0) {
        //已接受报价
        static NSString *sectionCell = @"SKUDefultQuestionTBNewCellText";
        SKUDefultQuestionTBNewCellText *cell = [self dequeueReusableCellWithIdentifier:sectionCell];
        if (cell == nil) {
            cell = [[SKUDefultQuestionTBNewCellText alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:sectionCell];
        }
        cell.selectionStyle = UITableViewCellSeparatorStyleNone;
        cell.model = self.model;
        //        [cell setIndexCell:indexPath.row];
        
        return cell;
    }else{
        
        static NSString *sectionCell = @"SKUDefultQuestionUserimageNewTBCell";
        SKUDefultQuestionUserimageNewTBCell *cell = [self dequeueReusableCellWithIdentifier:sectionCell];
        if (cell == nil) {
            cell = [[SKUDefultQuestionUserimageNewTBCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:sectionCell];
        }
        cell.selectionStyle = UITableViewCellSeparatorStyleNone;
        cell.model = self.model;
        if (index == 1) {
            //收到
            [cell setIndexCell:1];
        }else{
            //提醒
            [cell setIndexCell:2];
        }
        return cell;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [tableView deselectRowAtIndexPath:indexPath animated:NO];

    [self indexPathTbv:indexPath];
    
    
  
    
//    if (indexPath.row == 1) {
//
//        int conut =  [self.model.acceptPriceCount intValue];
//        if (conut>0) {
//            [self pushVc:0];
//             return;
//        }
//
//        conut =  [self.model.receivePriceCount intValue];
//        if (conut>0) {
//            [self pushVc:1];
//             return;
//        }
//
//        conut =  [self.model.openSourceCount intValue];
//        if (conut>0) {
//            [self pushVc:2];
//             return;
//        }
//    }else if (indexPath.row ==2){
//        int conut =  [self.model.receivePriceCount intValue];
//        if (conut>0) {
//            [self pushVc:1];
//             return;
//        }
//
//        conut =  [self.model.openSourceCount intValue];
//        if (conut>0) {
//            [self pushVc:2];
//             return;
//        }
//    }else if (indexPath.row == 3){
//        [self pushVc:2];
//         return;
//    }
}


//-(void)indexPathTbv:(NSIndexPath *)indexPath{
//
//    if (indexPath.row == 0) {
//
//    }else{
//
//        if (indexPath.row == 1) {
//
//            int conut =  [self.model.acceptPriceCount intValue];
//            if (conut>0) {
//                [self pushVc:0];
//                return;
//            }
//
//            conut =  [self.model.receivePriceCount intValue];
//            if (conut>0) {
//                [self pushVc:1];
//                return;
//            }else{
//
//                return;
//            }
//
//        }else if (indexPath.row == 2){
//            int conut =  [self.model.acceptPriceCount intValue];
//            if (conut>0) {
//                conut =  [self.model.receivePriceCount intValue];
//                if (conut>0) {
//                    [self pushVc:1];
//                    return;
//
//                }else{
//
//                    return;
//                    //                    return [self getUITableViewCell:2];
//                }
//
//            }else{
//                [self pushVc:2];
//                return;
//            }
//
//        }else{
//
//            [self pushVc:2];
//            return;
//        }
//    }
//}


-(void)indexPathTbv:(NSIndexPath *)indexPath{
    
    bool acceptPrice =  [self.model.acceptPriceCount intValue]>0;
    bool openSource =  [self.model.openSourceCount intValue]>0;
    
    
    if (indexPath.row == 0) {

    }else if (indexPath.row == 1){
        if (acceptPrice) {
            [self pushVc:0];
            
        }else{
            if (openSource) {
                //0 已接受报价 1 收到 2提醒
                
                [self pushVc: 2];

            }else{
                
//                [self pushVc: 2];
            }
        }
    }else if (indexPath.row == 2){
        if (acceptPrice) {
            if (openSource) {
                [self pushVc: 2];
            }else{

            }
        }else{

        }
    }else{
        if (acceptPrice && indexPath.row ==3) {

        }else{

        }
    }
}

-(void)pushVc:(NSInteger)index
{
    if (index == 0) {
        SKUDetailTakeinVC *vc = [[SKUDetailTakeinVC alloc]init];
        vc.index=4;
        vc.leb.text = @"接受的报价";
        vc.goodsId = self.model.goodsId;
        [self.getNav pushViewController:vc animated:YES];
    }else if (index == 1) {
        SKUDetailReceiveVC *vc = [[SKUDetailReceiveVC alloc]init];
        vc.indexType = @"1";
        vc.userEnquiryId = self.model.userEnquiryId;
        vc.goodsid = self.model.goodsId;
        vc.str = @"提醒商家报价";
        vc.leb.text = @"收到的报价";
        [self.getNav pushViewController:vc animated:YES];
        
    }else{
        SKUDetailReceiveVC *vc = [[SKUDetailReceiveVC alloc]init];
        vc.indexType = @"2";
        vc.str = @"提醒商家报价";
        vc.leb.text = @" ";
        vc.goodsid = self.model.goodsId;
        vc.IsEnquiry = ![self.model.myEnquiryFlag isEqualToString:@"1"];
        [self.getNav pushViewController:vc animated:YES];
    }
   
}



//设置表头高度
//- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
//{
//    return 200;
//}

//添加标头中的内容
//-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
//{
//    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenW, 200)];
//
//    BaseLab *labName = [[BaseLab alloc]initializeFrame:CGRectMake(15, 450, 242, 33) text:@"30003/000P-8200" font:10 textColor:[UIColor blackColor] isBold:NO numberOfLines:1];
//    labName.font = [UIFont fontWithName:@"CSD-Block-Bold" size:40];
//    [view addSubview:labName];
//    [labName mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.equalTo(view).offset(15);
//        make.top.equalTo(view.mas_top).offset(30);
//    }];
//
//    BaseLab *anotherName = [[BaseLab alloc]initializeFrame:CGRectMake(15, 486, 28, 14) text:@"绿鬼" font:10 textColor:[UIColor blackColor] isBold:NO numberOfLines:1];
//    anotherName.font = [UIFont fontWithName:@"PingFang-SC-Regular" size:14];
//
//    [view addSubview:anotherName];
//    [anotherName mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.equalTo(view).offset(15);
//        make.top.equalTo(labName.mas_bottom).offset(5);
//    }];
//
//
//
//
//    UILabel *libelAge = [[UILabel alloc]initWithFrame:CGRectMake(20, 120, 10+[NSString textWidth:self.model.skuTicket font:20 height:20], 20)];
//    libelAge.text = self.model.skuTicket;
//    libelAge.textColor = [UIColor blackColor];
//    [view addSubview:libelAge];
//    [libelAge mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.equalTo(view).offset(15);
//        make.top.equalTo(anotherName.mas_top).offset(35);
//    }];
//
//    BaseImageV *image = [[BaseImageV alloc]initializeFrame:CGRectMake(0, 0, 1, 1) contentMode:2];
//    [image setImage:[UIImage imageNamed:@"sku_icon_tickect"]];
//    [view addSubview:image];
//    [image mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.equalTo(libelAge.mas_right).offset(10);
//        make.top.equalTo(libelAge.mas_top).offset(0);
//        make.width.height.mas_equalTo(20);
//    }];
//
//    UILabel *libelAge1 = [[UILabel alloc]initWithFrame:CGRectMake(20, 45+105, [NSString textWidth:@"每次刷新询价可以获得0.5张询价票" font:20 height:20], 14)];
//    NSString *str = [NSString stringWithFormat:@"每次刷新询价可以获得%@张询价票",self.model.skuTicketReward];
//    libelAge1.text = str;
//    libelAge1.font = [UIFont systemFontOfSize:14];
//    libelAge1.textColor = [UIColor blackColor];
//    [view addSubview:libelAge1];
//
//    [libelAge1 mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.equalTo(view).offset(15);
//        make.top.equalTo(libelAge.mas_bottom).offset(10);
//    }];
//
//
//    MainRefreshBezierPathV *RefreshV = [[MainRefreshBezierPathV alloc]initWithFrame:CGRectMake(kScreenW-50,20+90, 32, 32)];
//    RefreshV.model = self.model;
//    RefreshV.radius =  [self.model.refreshTime convertCurrentTimeChangeHours];
//    [view addSubview:RefreshV];
//
//    return view;
//}



- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    
#if !defined(ST_PULLTOREFRESH_HEADER_HEIGHT)
        STRefreshHeader * header = self.header;
        CGFloat orginY = - (header.st_height + self.swipeTableView.swipeHeaderView.st_height + self.swipeTableView.swipeHeaderBar.st_height);
        if (header.st_y != orginY) {
            header.st_y = orginY;
        }
#endif
    
//    CGFloat sectionHeaderHeight = 200;
//    
//    if(scrollView.contentOffset.y<=sectionHeaderHeight&&scrollView.contentOffset.y>=0) {
//        
//        scrollView.contentInset = UIEdgeInsetsMake(-scrollView.contentOffset.y, 0, 0, 0);
//        
//    } else if (scrollView.contentOffset.y>=sectionHeaderHeight) {
//        
//        scrollView.contentInset = UIEdgeInsetsMake(-sectionHeaderHeight, 0, 0, 0);
//    }
    
}


@end
